package visualization.graphkeymodel.circlechart;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import visualization.PairEntry;
import visualization.Visualization;
import visualization.graphkeymodel.GraphKeyModel;


/**
 *
 */
@SuppressWarnings("serial")
/**
 *
 * @author Ben Schwab
 * Creates a circle chart where data is represented by relative circle
 * sizes and encoded in a color key.
 *
 */
public class CircleChart extends GraphKeyModel {
    private static final int ROW_SIZE = 3;
    private static final int COLUMN_BUFFER = 10;
    private static final int ROW_BUFFER = 10;
    private ArrayList<RelativeCircle> myCircles = new ArrayList<RelativeCircle>();
    private double myLargestValue;
    private List<PairEntry<String, Double>> myEntries;

    /**
     * @param entries list of pairs
     * @param preferredDimension preferred dimension
     */
    public CircleChart (List<PairEntry<String, Double>> entries, Dimension preferredDimension) {
        super(preferredDimension);
        myEntries = entries;
        calculateAreas();
        createNewColorGenerator(entries.size());
        generateRandomColors();
        generateCircles(entries);
        generateColorKey();
        generateTitle();
    }

    private void generateCircles (List<PairEntry<String, Double>> entries) {
        myLargestValue = 0;
        for (PairEntry<String, Double> curEntry : entries) {
            if (curEntry.getMyInfo() > myLargestValue) {
                myLargestValue = curEntry.getMyInfo();
            }
        }
        int counter = 0;
        int startX = (int) getGraphArea().getMinX();
        int startY = (int) getGraphArea().getMinY();
        int chartWidth = (int) getGraphArea().getWidth();
        int circleWidth = chartWidth / ROW_SIZE;
        int circleHeight = circleWidth;
        int curX = startX;
        int curY = startY;
        int rowCounter = 0;
        Color[] myColors = getColors();
        for (PairEntry<String, Double> curEntry : entries) {
            Rectangle curRectangle = new Rectangle(curX, curY, circleWidth, circleHeight);
            double curValue = curEntry.getMyInfo();
            double ratio = curValue / myLargestValue;
            myCircles.add(new RelativeCircle(curRectangle, myColors[counter], ratio));
            rowCounter++;
            counter++;
            if (rowCounter == ROW_SIZE) {
                rowCounter = 0;
                curY += circleHeight + ROW_BUFFER;
                curX = startX;
            }
            else {
                curX += circleWidth + COLUMN_BUFFER;
            }
        }
    }

    private void generateColorKey () {
        ArrayList<String> titles = new ArrayList<String>();
        for (PairEntry<String, Double> entry : myEntries) {
            titles.add(entry.getMyTitle());
        }
        super.generateColorKey(titles);
    }

    @Override
    public Visualization randomizeColors () {
        return new CircleChart(myEntries, getDimension());
    }

    @Override
    public void paintComponent (Graphics g) {
        super.paintComponent(g);
        Graphics2D pen = (Graphics2D) g;
        for (RelativeCircle curCircle : myCircles) {
            curCircle.draw(pen);
        }
        getColorKey().paint(pen);
        // getTitle().paint(pen);
    }

}
